//存放主要交互逻辑JS代码
//前端用cookie模拟校验
//javascript模块化
{
    var seckill = {
        URL : {
            nowTime : function () {
                return "/seckill/time/now";
            },
            exposer : function (seckillId) {
                return "/seckill/"+ seckillId + "/exposer";
            },
            execution : function (sekcillId, md5) {
                return "/seckill/" + sekcillId + "/" + md5  + "/execution";
            }

        },

        validatePhone : function (phone) {
            if(phone && phone.length == 11 && !isNaN(phone)) {
                return true;
            }else return false;
        },

        handleSeckill : function (sekcillId,node) {

            node.hide().html("<button class='btn btn-primary btn-lg' id='killBtn'>开始秒杀</button>");
            $.post(seckill.URL.exposer(sekcillId),{},function (result) {
                if(result && result["success"]) {
                    var exposer = result["data"];
                    if(exposer["exposed"]) {
                        var md5 =exposer["md5"];
                        var killUrl = seckill.URL.execution(sekcillId,md5);
                        $("#killBtn").one("click",function () {
                            $(this).addClass("disable");

                            $.post(killUrl,{},function (result) {
                                if(result && result["success"]) {
                                    var seckillExecution =  result["data"];
                                    var stateInfo = seckillExecution["stateInfo"];
                                    node.html("<span class='label-success'>"+ stateInfo +"</span>");
                                }else {
                                    //未注册
                                    seckill.detail.init({
                                        seckillId : seckillId,
                                        startTime : exposer["start"],  //转毫秒
                                        endTime : exposer["end"]
                                    })
                                }
                            },"json");
                        });
                        node.show();
                    }else {
                        //活动还未开启
                        var nowTime = exposer["now"];
                        var startTime = exposer["start"];
                        var endTime = exposer["end"];
                        $.get(seckill.URL.nowTime(),{},function (result) {
                            if(result && result["success"]) {
                                var nowTime =result["data"];
                                seckill.countdown(seckillId,nowTime,startTime,endTime);
                            }else {
                                console.log("result:"+ result);
                            }
                        });
                    }
                }
            },"json")
        },

        countdown : function (seckillId,nowTime,startTime,endTime) {

            var seckillBox = $("#seckill-box");
            var killTime = new Date(startTime + 1000);
            if(nowTime > endTime) {
                seckillBox.html("秒杀结束 ")
            } else if(nowTime < startTime){
                seckillBox.countdown(killTime,function (event) {
                    var format = event.strftime("倒计时 %D 天 %H 时 %M 分 %S 秒");
                    seckillBox.html(format);
                }).on("finish.countdown",function () {
                    seckill.handleSeckill(seckillId,seckillBox);
                })
            }else {
                seckill.handleSeckill(seckillId,seckillBox);
            }

        },



        detail :{
            init : function (params) {
                var killPhone = $.cookie("killPhone");
                var seckillId = params.seckillId;
                var startTime = params.startTime;
                var endTime = params.endTime;

                //判断是否登入
                if(!seckill.validatePhone(killPhone)) {
                    //没登入
                    var killPhoneModal = $("#killPhoneModal");

                    killPhoneModal.modal({
                       show : true,
                       backdrop : "static",
                       keyboard : false
                    });

                    $("#killPhoneBtn").click(function () {
                        var inputPhone = $("#killPhone").val();
                        if(seckill.validatePhone(inputPhone)) {
                            $.cookie("killPhone",inputPhone,{expires : 1,path : "/seckill"});
                            window.location.reload();
                        }else {
                            $("#killPHoneMessage").hide().html("<label class='label-danger'>手机号格式错误</label>").show(300);
                        }
                    });
                }else {
                    //已登入
                    $.get(seckill.URL.nowTime(),{},function (result) {
                        if(result && result["success"]) {
                            var nowTime =result["data"];
                            seckill.countdown(seckillId,nowTime,startTime,endTime);
                        }else {
                            console.log("result:"+ result);
                        }
                    },"json");

                }

            }
        }

    };
}